package game.map.generator;

import game.graphics.Block;

import java.io.Serializable;
import java.util.Random;

import data.DataManager;

/**
 * 
 * @author Drahomir Karchnak
 *
 */
public class TreeGenerator implements Serializable
{
	/**
	 * Constructor.
	 * Saves reference to the blocks.
	 * 
	 * @param blocks
	 */
	public TreeGenerator(Block blocks[][])
	{
		this.blocks		= blocks;
		
		this.mapWidth	= blocks.length;
		this.mapHeight	= blocks[0].length;
	}

	/**
	 * Hard to describe every part of this method properly
	 * maybe this image will help http://screenshot.cz/P5LOE/TreeGen.png
	 * 
	 * @param x
	 * @param y
	 */

	public void createTree(int x, int y){
		if(x < 3 | x > mapWidth - 3) return;
		blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_BOTTOM);
		
		boolean stop = false;
		int rand = new Random().nextInt(3);
		y--;
		
		switch(rand){
			case 0:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE_LEFT);
				blocks[x-1][y].setBlockType(DataManager.BlockType.BLOCK_TREE_LEAVES_LEFT);
			break;
			
			case 1:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE);
			break;
			
			case 2:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE_RIGHT);
				blocks[x+1][y].setBlockType(DataManager.BlockType.BLOCK_TREE_LEAVES_RIGHT);
			break;
		}
		
		y--;
		blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE);
		
		rand = new Random().nextInt(4);
		y--;
		
		switch(rand){
			case 0:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE_LEFT);
				blocks[x-1][y].setBlockType(DataManager.BlockType.BLOCK_TREE_LEAVES_LEFT);
			break;
			
			case 1:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE);
			break;
			
			case 2:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE_RIGHT);
				blocks[x+1][y].setBlockType(DataManager.BlockType.BLOCK_TREE_LEAVES_RIGHT);
			break;
			
			case 3:
				createTreeTop(x,y);	
				stop = true;
			break;
		}
		
		if(stop) return;
		y--;
		blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE);
		y--;

		switch(rand){
			case 0:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE_LEFT);
				blocks[x-1][y].setBlockType(DataManager.BlockType.BLOCK_TREE_LEAVES_LEFT);
			break;
			
			case 1:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE);
			break;
			
			case 2:
				blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREE_MIDDLE_RIGHT);
				blocks[x+1][y].setBlockType(DataManager.BlockType.BLOCK_TREE_LEAVES_RIGHT);
			break;
			
			case 3:
				createTreeTop(x,y);	
				stop = true;
			break;
		}
		
		if(stop) return;
		y--;
		createTreeTop(x,y);
	}
	
	private void createTreeTop(int x, int y){
		blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_BOTTOM_MIDDLE);
		blocks[x-1][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_BOTTOM_LEFT);
		blocks[x+1][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_BOTTOM_RIGHT);
		y--;
		blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_MIDDLE_MIDDLE);
		blocks[x-1][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_MIDDLE_LEFT);
		blocks[x+1][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_MIDDLE_RIGHT);
		y--;
		blocks[x][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_TOP_MIDDLE);
		blocks[x-1][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_TOP_LEFT);
		blocks[x+1][y].setBlockType(DataManager.BlockType.BLOCK_TREETOP_TOP_RIGHT);
	}
	
	private Block	blocks[][];
	private int		mapWidth;
	private int		mapHeight;
}
